package drds.binlog.store;

import drds.binlog.common.position.EntryPosition;
import drds.binlog.common.position.LogPosition;
import drds.binlog.store.model.Event;
import org.apache.commons.lang.StringUtils;

/**
 * 相关的操作工具
 */
public class LogPositions
{

    /**
     * 找出一个最小的position位置，相等的情况返回position1
     */
    public static LogPosition min(LogPosition position1, LogPosition position2)
    {
        if (position1.getLogIdentity().equals(position2.getLogIdentity()))
        {
            // 首先根据文件进行比较
            if (position1.getEntryPosition().getJournalName().compareTo(position2.getEntryPosition().getJournalName()) > 0)
            {
                return position2;
            } else if (position1.getEntryPosition().getJournalName().compareTo(position2.getEntryPosition().getJournalName()) < 0)
            {
                return position1;
            } else
            {
                // 根据offest进行比较
                if (position1.getEntryPosition().getPosition() > position2.getEntryPosition().getPosition())
                {
                    return position2;
                } else
                {
                    return position1;
                }
            }
        } else
        {
            // 不同的主备库，根据时间进行比较
            if (position1.getEntryPosition().getTimestamp() > position2.getEntryPosition().getTimestamp())
            {
                return position2;
            } else
            {
                return position1;
            }
        }
    }

    /**
     * 根据entry创建对应的Position对象
     */
    public static LogPosition createPosition(Event event)
    {
        EntryPosition position = new EntryPosition();
        position.setJournalName(event.getJournalName());
        position.setPosition(event.getPosition());
        position.setTimestamp(event.getExecuteDateTimeLong());

        position.setServerId(event.getServerId());

        position.setGtid(event.getGtid());

        LogPosition logPosition = new LogPosition();
        logPosition.setEntryPosition(position);
        logPosition.setLogIdentity(event.getLogIdentity());
        return logPosition;
    }

    /**
     * 根据entry创建对应的Position对象
     */
    public static LogPosition createPosition(Event event, boolean included)
    {
        EntryPosition position = new EntryPosition();
        position.setJournalName(event.getJournalName());
        position.setPosition(event.getPosition());
        position.setTimestamp(event.getExecuteDateTimeLong());
        position.setIncluded(included);

        LogPosition logPosition = new LogPosition();
        logPosition.setEntryPosition(position);
        logPosition.setLogIdentity(event.getLogIdentity());
        return logPosition;
    }

    /**
     * 判断当前的entry和position是否相同
     */
    public static boolean checkPosition(Event event, LogPosition logPosition)
    {
        EntryPosition entryPosition = logPosition.getEntryPosition();
        boolean result = entryPosition.getTimestamp().equals(event.getExecuteDateTimeLong());

        boolean exactely = (StringUtils.isBlank(entryPosition.getJournalName()) && entryPosition.getPosition() == null);
        if (!exactely)
        {// 精确匹配
            result &= entryPosition.getPosition().equals(event.getPosition());
            if (result)
            {// short path
                result &= StringUtils.equals(event.getJournalName(), entryPosition.getJournalName());
            }
        }

        return result;
    }
}
